from datetime import datetime
from elasticsearch_dsl import DocType, Date, Nested, Boolean, \
    analyzer, Completion, Keyword, Text, Integer
from elasticsearch_dsl.connections import connections
from elasticsearch_dsl.analysis import CustomAnalyzer as _CustomAnalyzer

connections.create_connection(hosts=["localhost"])


class CustomAnalyzer(_CustomAnalyzer):
    def get_analysis_definition(self):
        return {}


ik_analyzer = CustomAnalyzer("ik_max_word", filter=["lowercase"])


class WuYictoIndex(DocType):
    # 创建51cto索引类型
    suggest = Completion(analyzer=ik_analyzer)
    url = Keyword()
    url_object_id = Keyword()
    front_image_url = Keyword()
    front_image_path = Keyword()
    title = Text(analyzer="ik_max_word")
    create_time = Date()
    author = Keyword()
    praise_num = Integer()
    content = Text(analyzer="ik_max_word")
    origin = Keyword()
    origin_url = Keyword()

    class Meta:
        index = "51cto"
        doc_type = "article"

class TodayJobIndex(DocType):
    # 创建51cto索引类型
    suggest = Completion(analyzer=ik_analyzer)
    url = Keyword()
    url_object_id = Keyword()
    title = Text(analyzer="ik_max_word")
    publish_time = Date()
    job_addr = Keyword()
    salary_min = Integer()
    salary_max = Integer()
    job_desc = Text(analyzer="ik_max_word")
    company_name = Keyword()
    company_url = Keyword()

    class Meta:
        index = "today_job"
        doc_type = "job"


class GuokrIndex(DocType):
    # 创建51cto索引类型
    suggest = Completion(analyzer=ik_analyzer)
    url = Keyword()
    url_object_id = Keyword()
    title = Text(analyzer="ik_max_word")
    create_time = Date()
    content = Keyword()
    tags = Text(analyzer="ik_max_word")
    origin = Keyword()
    origin_url = Keyword()
    answer_content = Text(analyzer="ik_max_word")

    class Meta:
        index = "guokr"
        doc_type = "question"


if __name__ == '__main__':
    WuYictoIndex.init()
